<?php

namespace app\admin\model;

use think\Model;

class Cate extends Model
{   
    public function getTypeAttr($value)
    {
        $type = [1=>'列表栏目',2=>'频道封面',3=>'外链栏目'];
        return $type[$value];
    }
    /**
     * 栏目排序
     *
     * @return void
     */
    public function cateSort(){
        $cate=self::order('sort desc')->all();
        return  $this->getCateAll($cate);
    }
     /**
     * 获取栏目列表-优化
     * @param $data 包含 所有栏目分类的数组
     * @param int $pid  上级栏目标识
     * @param int $level 层级数
     * @return array
     */
    public static function getCateAll($data,$pid=0,$level=-1){
       static $arr=array();
       $level+=1;
       if ($level==0) {
          $str="";
       }else{
           $str="|";
       }
       foreach ($data as $v) {
           if ($pid==$v['pid']) {
               $tmp_arr=array();
               $tmp_arr['id']=$v['id'];
               $tmp_arr['pid']=$v['pid'];
               $tmp_arr['catename']=$str.str_repeat('-----',$level).$v['catename'];
               $tmp_arr['sort']=$v['sort'];
               $tmp_arr['type']=$v['type'];
               $tmp_arr['isshow']=$v['isshow'];
                $tmp_arr['model_id']=$v['model_id'];
             
               $arr[]=$tmp_arr;
               self::getCateAll($data,$v['id'],$level);
               unset($v);
           }
       }
        return $arr;
    }
    // 获取子栏目id
    public function getChildIds($id){
        $res=model('Cate')->where('pid',$id)->column('id');
        return $res;
    }

}
